//
//  ViewController.m
//  Study1
//
//  Created by Zeak on 2017/2/15.
//  Copyright © 2017年 Zeak. All rights reserved.
//

#import "ViewController.h"
#import "UIView+YZTCView.h"
#import "ImageLabelView.h"
#import "CollectionViewCell.h"
#import "CollectionReusableView.h"
#define SCREEN_WIDTH   [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT   [UIScreen mainScreen].bounds.size.height
#define LEFT_IMG_WIDTH  37
#define RIGHT_IMG_WIDTH 26
#define IMGHEADVIEW_HEIGHT 183 + 152
#define IMG_WIDTH 32.7
#define SMALL_IMG_WIDTH ([UIScreen mainScreen].bounds.size.width-24) / 2
#define SMALL_IMG_HEIGHT 100

@interface ViewController ()
<
UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout
>
{
    
    UIView *_naviView;
    UICollectionView *_collectionView;

}

@end

@implementation ViewController
static NSString *const cellId = @"cellId";
static NSString *const headerId = @"headerId";

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self naviCreat];
    [self loadCollectionView];
    


    
    
    
}


-(void)naviCreat{
    
    _naviView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 49)];
    
    UIImage *letfNaviImg = [UIImage imageNamed:@"图层31"];
    UIImageView *leftImgView = [[UIImageView alloc]initWithFrame:CGRectMake(14,5, LEFT_IMG_WIDTH , LEFT_IMG_WIDTH)];
    leftImgView.image = letfNaviImg;
    [_naviView addSubview:leftImgView];
    UITextField *txtField = [[UITextField alloc]initWithFrame:CGRectMake(leftImgView.yzRight + 10, 10, 209, 27)];
    txtField.placeholder = @"二维码是什么原理";
    txtField.borderStyle = UITextBorderStyleLine;
    [_naviView addSubview:txtField];
    UIImage *rightNaviImg = [UIImage imageNamed:@"图层30"];
    UIImageView *rightImgView = [[UIImageView alloc]initWithFrame:CGRectMake(txtField.yzRight +14,11,26,26)];
    rightImgView.image = rightNaviImg;
    [_naviView addSubview:rightImgView];
    
    //添加“导航栏”
    [self.view addSubview:_naviView];
    
}



-(void)loadCollectionView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SMALL_IMG_WIDTH, SMALL_IMG_HEIGHT + 40);
    layout.headerReferenceSize=CGSizeMake(SCREEN_WIDTH, 584);//头视图的大小
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 49 + 20 , SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 20) collectionViewLayout: layout];
    _collectionView.backgroundColor = [UIColor redColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    
    [self.view addSubview:_collectionView];
    
    // 注册cell、sectionHeader
    [_collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:cellId];
    [_collectionView registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId];
}


#pragma mark ---- UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor purpleColor];
    // 具体设置
    
    return cell;

}

// 和UITableView类似，UICollectionView也可设置段头段尾
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    
    if (kind == UICollectionElementKindSectionHeader) {
        
        CollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId forIndexPath:indexPath];
        reusableview = headerView;
    }
    return reusableview;
}


#pragma mark ---- UICollectionViewDelegateFlowLayout


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(11, 8, 0, 8);
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5.f;
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 5.f;
}


//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
//{
//    return (CGSize){SCREEN_WIDTH,584};
//}




@end
